const { join } = require('path');
const D = require('./webpack.build.module.conf');
const special = require('../proxy/proxy');


//TODO: 设置端口与模块名称
const debugModule = 'binge'; //要打包的模块
const devPort = 9001; //测试端口
const proxyStaticUrl="https://iot.ktuntech.com";//静态代理地址
const proxyDataUrl="https://iot.ktuntech.com"; //数据代理
const dataUrl="/iot";//数据请求前缀
const domain = ""; //二级域名

const specialProxy = {};
Object.keys(special).forEach(m=>{
	specialProxy[`${domain}${m}`] = {
		secure:true,
		changeOrigin:true,
		target:'http://localhost:'+devPort,
		pathRewrite: {[`${domain}${m}`]: [`${domain}/req${special[m]}`]}
	};
})

module.exports = function () {
	return {
		devServer: {
			clientLogLevel: 'error',
			disableHostCheck: true,
			hot: false,
			contentBase: [join(__dirname, '../static'),join(__dirname, '../proxy')],//false, // since we use CopyWebpackPlugin.
			compress: false,
			host: 'localhost',
			port: devPort,
			inline:true,
			open: false,
			overlay: { warnings: false, errors: true },
			publicPath: `${domain}/modules`,
			proxy: {
				...specialProxy,
				[`${domain}/socket.io`]:{
					secure:true,
					changeOrigin:true,
					ws: true,
					target:proxyDataUrl
				},
				[`${dataUrl}`]:{
					secure:true,
					changeOrigin:true,
					target:proxyDataUrl
				},
				[`${domain}/modules/${debugModule}/index.js`]:{
					secure:true,
					changeOrigin:true,
					target:'http://localhost:'+devPort,
					pathRewrite: {[`${domain}/modules/${debugModule}/index.js`]: [`${domain}/modules/${debugModule}/${debugModule}.js`]}
				},
				[`!${domain}/(modules/${debugModule}|req)/**/*`]:{
					secure:true,
					changeOrigin:true,
					target:proxyStaticUrl
				}
			},//config.dev.proxyTable,
			quiet: true, // necessary for FriendlyErrorsPlugin
			watchOptions: {
				poll: false
			}
		},
		...D(debugModule,false),
		mode: "development",
		devtool: 'cheap-module-eval-source-map'
	}
}